package com.yanqu.road.dao.impl.activity.feast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.feast.data.CrossFeastPvpCampScoreRecord;
import com.yanqu.road.entity.activity.feast.data.CrossFeastPvpData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFeastPvpDataDaoImpl extends TempDataObjectDao<CrossFeastPvpData> {

    public static String getCrossFeastPvpDataSql = "select * from `t_c_feast_pvp_data` where `activityId`=?";

    public CrossFeastPvpData getCrossFeastPvpData(int activityId) {
        CrossFeastPvpData crossFeastPvpData = null;
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFeastPvpDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                crossFeastPvpData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return crossFeastPvpData;
    }

    public static String insertCrossFeastPvpDataSql = "insert into `t_c_feast_pvp_data`(activityId,lockState,round,updateTime,settle) values (?,?,?,?,?)";

    @Override
    protected boolean addData(CrossFeastPvpData data) {

        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSettle()));
        if (getDb().executeNoneQuery(insertCrossFeastPvpDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossFeastPvpDataSql = "update `t_c_feast_pvp_data` set lockState=?,round=?,updateTime=?,settle=? where activityId=? ";

    @Override
    protected boolean updateData(CrossFeastPvpData data) {

        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSettle()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateCrossFeastPvpDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossFeastPvpData parseData(ResultSet rs) throws SQLException {
        CrossFeastPvpData data = new CrossFeastPvpData();
        data.setActivityId(rs.getInt("activityId"));
        data.setLockState(rs.getBoolean("lockState"));
        data.setRound(rs.getInt("round"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setSettle(rs.getBoolean("settle"));
        return data;
    }
}
